﻿Imports Configuration.Templating.Context
Imports Configuration.Templating.Scope
Imports Aricie.DNN.UI.WebControls
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports System.Linq


Namespace Aricie.LuceneSearch.Settings.Templates
    Public Class TemplateEdition
        Inherits PropertyEditorUserControlBase

        Private NeedsBinding As Boolean = False

        Public ReadOnly Property TemplateConfiguration As Aricie.DNN.Modules.LuceneSearch.Settings.Templates
            Get
                Return CType(Me.Value, Aricie.DNN.Modules.LuceneSearch.Settings.Templates)
            End Get
        End Property

        Public ReadOnly Property TemplateType() As String
            Get
                If ddlType.SelectedValue = Aricie.DNN.Modules.LuceneSearch.Settings.ResultsControlMode.TemplateView.ToString Then
                    Return TemplateTypes.Template.ToString()
                End If
                Return TemplateTypes.Grid.ToString()
            End Get
        End Property

        Private Sub BindTemplates()
            ddlLanguage.Enabled = cbLocalize.Checked

            Dim ResultTemplates As New List(Of ITemplatingContext)
            Dim DetailsTemplates As New List(Of ITemplatingContext)

            Dim CurrentLanguage As String = ddlLanguage.SelectedValue
            If (Not cbLocalize.Checked) Then
                CurrentLanguage = String.Empty
            End If

            If cbPerContent.Checked Then
                For Each nadn In RetrieveContentProviderList()
                    ResultTemplates.Add(New DNNTemplatingContext(TemplateType) With {.CurrentCulture = CurrentLanguage, .Content = nadn.Name, .ContentDisplayName = nadn.DisplayName})
                Next

                For Each nadn In RetrieveDetailsEnabledContentProviderList()
                    DetailsTemplates.Add(New DNNTemplatingContext(TemplateTypes.Details.ToString()) With {.CurrentCulture = CurrentLanguage, .Content = nadn.Name, .ContentDisplayName = nadn.DisplayName})
                Next
            Else
                ResultTemplates.Add(New DNNTemplatingContext(TemplateType) With {.CurrentCulture = CurrentLanguage, .Content = String.Empty, .ContentDisplayName = String.Empty})
                If (RetrieveDetailsEnabledContentProviderList.Count > 0) Then
                    DetailsTemplates.Add(New DNNTemplatingContext(TemplateTypes.Details.ToString()) With {.CurrentCulture = CurrentLanguage, .Content = String.Empty, .ContentDisplayName = String.Empty})
                End If
            End If

            rTemplates.DataSource = ResultTemplates
            rTemplates.DataBind()

            pnlDetailsTemplates.Visible = DetailsTemplates.Count > 0
            rDetailsTemplates.DataSource = DetailsTemplates
            rDetailsTemplates.DataBind()
        End Sub

        Private Function RetrieveContentProviderList() As IEnumerable(Of Configuration.ContentManagement.ContentProviders.IContentProvider)
            Return Configuration.ContentManagement.ContentProviderLibrary.Instance.ContentProviders
        End Function

        Private Function RetrieveDetailsEnabledContentProviderList() As IEnumerable(Of Configuration.ContentManagement.ContentProviders.IContentProvider)
            Return Configuration.ContentManagement.ContentProviderLibrary.Instance.DetailedContentProviders
        End Function

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                DotNetNuke.Services.Localization.Localization.LoadCultureDropDownList(ddlLanguage, DotNetNuke.Services.Localization.CultureDropDownTypes.DisplayName, String.Empty)
            End If
            BindData()
            'End If
            AddHandler Page.LoadComplete, AddressOf PageLoadCompleted
        End Sub

        Private Sub PageLoadCompleted(ByVal sender As Object, ByVal e As EventArgs)
            If NeedsBinding Then
                BindData()
            End If
        End Sub

        Private Sub BindData()
            ddlType.DataSource = System.Enum.GetValues(GetType(Aricie.DNN.Modules.LuceneSearch.Settings.ResultsControlMode))
            ddlType.DataBind()
            ddlType.SelectedValue = TemplateConfiguration.ResultsControlMode.ToString()

            tbControl.Text = TemplateConfiguration.ResultsControlPath

            cbLocalize.Checked = TemplateConfiguration.UseLocalizedTemplates
            cbPerContent.Checked = TemplateConfiguration.UseModuleBasedTemplates

            tbControl.Visible = TemplateConfiguration.ResultsControlMode = Aricie.DNN.Modules.LuceneSearch.Settings.ResultsControlMode.CustomView
            lblControl.Visible = tbControl.Visible
            pnlGlobal.Visible = TemplateConfiguration.ResultsControlMode <> Aricie.DNN.Modules.LuceneSearch.Settings.ResultsControlMode.CustomView

            BindTemplates()
        End Sub

        Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbLocalize.CheckedChanged, cbPerContent.CheckedChanged
            TemplateConfiguration.UseLocalizedTemplates = cbLocalize.Checked
            TemplateConfiguration.UseModuleBasedTemplates = cbPerContent.Checked
            NeedsBinding = True
        End Sub

        Private Sub rTemplates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rTemplates.ItemDataBound, rDetailsTemplates.ItemDataBound
            DirectCast(e.Item.FindControl("tiCurrent"), TemplateItem).SetupTemplating(DirectCast(e.Item.DataItem, DNNTemplatingContext), CurrentScope.GetScope())
        End Sub

        Private Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.SelectedIndexChanged
            NeedsBinding = True
        End Sub

        Private Sub CurrentScope_ScopeChangedEvent(ByVal ea As System.EventArgs) Handles CurrentScope.ScopeChangedEvent
            NeedsBinding = True
        End Sub

        Private Sub ddlType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlType.SelectedIndexChanged
            TemplateConfiguration.ResultsControlMode = DirectCast([Enum].Parse(GetType(Aricie.DNN.Modules.LuceneSearch.Settings.ResultsControlMode), ddlType.SelectedValue), Aricie.DNN.Modules.LuceneSearch.Settings.ResultsControlMode)
            NeedsBinding = True
        End Sub

        Private Sub tbControl_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbControl.TextChanged
            TemplateConfiguration.ResultsControlPath = tbControl.Text
        End Sub

        Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            If TrialMode IsNot Nothing Then
                cbLocalize.Enabled = False
                cbLocalize.ToolTip = TrialMode.Message
                ddlLanguage.Enabled = False
                ddlLanguage.ToolTip = TrialMode.Message

                If (cbPerContent.Checked) Then
                    ' on désactive les contrôles des repeaters après le premier
                    For ControlCounter As Integer = 1 To rTemplates.Controls.Count - 1
                        DirectCast(rTemplates.Controls(ControlCounter).Controls(1), TemplateItem).DisableForTrial(TrialMode)
                    Next

                    For ControlCounter As Integer = 1 To rDetailsTemplates.Controls.Count - 1
                        DirectCast(rDetailsTemplates.Controls(ControlCounter).Controls(1), TemplateItem).DisableForTrial(TrialMode)
                    Next
                End If
            End If
        End Sub
    End Class
End Namespace